package com.zbkj.crmeb.task.tools;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zbkj.crmeb.merchant.api.service.MerUserCenterService;
import com.zbkj.crmeb.user.model.User;
import com.zbkj.crmeb.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 功能描述: 删除未授权手机号的用户
 * @Author: wangzh
 * @Date: 2022/12/15 16:20
 */
@Slf4j
@Component
@Configuration
@EnableScheduling
public class RemoveUser {

    @Autowired
    private MerUserCenterService merUserCenterService;
    @Autowired
    private UserService userService;

    /**
     * 功能描述: 删除没有授权手机号用户
     * @Return: void
     * @Author: wangzh
     * @Date: 2022/12/15 16:22
     */
    @XxlJob("removeUser")
    public void init(){
        try {
            List<User> list = userService.list(Wrappers.<User>lambdaQuery()
                    .eq(User::getMerId, 87)
                    .gt(User::getCreateTime, "2022-10-01")
                    .isNull(User::getPhone));
            if (CollectionUtils.isNotEmpty(list)){
                List<Integer> uids = list.stream().map(User::getUid).collect(Collectors.toList());
                for (Integer uid : uids){
                    merUserCenterService.removeUser(uid);
                }
            }
        } catch (Exception e){
            e.printStackTrace();
            log.error("RemoveUser.task" + " | msg : " + e.getMessage());
        }
    }
}
